<template>
  <a-modal
    :title="$t('清除条件')"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    v-drag
  >
    <div>
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('库存数量')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cond">
              <a-radio-group v-model="model.cond" @change="changeCond">
                <a-radio v-for="(item,i) in conds" :key="i" :value="item.value">{{ item.text }}</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row style="margin-bottom: 20px;text-align: center;">
          <a-col :span="24">
            <span style="margin-left: 30px; color: orange">
              {{ $t('为了您的数据完整性，请谨慎操作，一旦删除将不可恢复!') }}
            </span>
          </a-col>
        </a-row>
        <a-row style="text-align: center;">
          <a-col :span="24">
            <span style="margin-left: 30px; color: orange">
              {{ $t('点击“确定”按钮将执行删除，点击“取消”放弃本次操作。') }}
            </span>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <template slot="footer">
      <a-button @click="handleCancel">{{ $t('关闭') }}</a-button>
      <a-popconfirm :title="$t('clear-tip',{cond: model.cond_dictText})" @confirm="handleOk">
        <a-button type="primary">{{ $t('确定') }}</a-button>
      </a-popconfirm>
    </template>
  </a-modal>
</template>

<script>
import Vue from 'vue'
import { TENANT_ID } from '@/store/mutation-types'
import { deleteAction } from '@/api/manage'

export default {
  name: 'GoodsStockClearModal',
  data() {
    return {
      width: 680,
      visible: false,
      confirmLoading: false,
      model:{
        cond:'',
        cond_dictText: ''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
      url: {
        delete: '/goods/bisGoodsStock/clear',
      },
      validatorRules: {
        cond: [{ required: true, message: this.$t('请选择') }],
      },
      tenantid: Vue.ls.get(TENANT_ID),
      conds:[{value:'0',text:this.$t('等于0')},{value:'1',text:this.$t('小于0')},{value:'2',text:this.$t('小于等于0')},{value:'3',text:this.$t('全部')}]
    }
  },
  methods: {
    changeCond(e){
      this.$set(this.model,'cond_dictText',this.conds.filter(c=>c.value == this.model.cond)[0].text)
    },
    edit() {
      this.visible = true
      this.model.cond=''
      this.model.cond_dictText=''
    },
    handleOk() {
      let that = this
      // 触发表单验证
      that.$refs.form.validate((valid) => {
        if (valid) {
          deleteAction(that.url.delete, that.model)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.visible = false
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.visible = false
    }
  }
}
</script>
<style>
  .batchForm .ant-form-item-label{
    width: 30%;
  }
  .batchForm .ant-form-item-control-wrapper{
    width: 62.5%;
  }
</style>